package com.projectsquirrel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class NewObservation5 extends Activity  {
	private LocationManager myManager;

	private Observation newObs;
	    
	private static final int PREFERENCES_GROUP_ID = 11;
	private static final int PAGE_2 = 12;
	private static final int PAGE_3 = 13;
	private static final int PAGE_4 = 14;
	private static final int PAGE_5 = 15;
	private static final int PAGE_6 = 16;
	private static final int PAGE_7 = 17;
	
	private Button nextObsButton5;
    private TextView completitionStatus;
    private Context context;
    private SharedPreferences pref;
    
    private RadioGroup nut_Tree_Group;
    private RadioGroup seed_Tree_Group;
    private RadioGroup fruit_Tree_Group;
    private RadioGroup tiny_Tree_Group;
    private RadioGroup cone_Tree_Group;
    private EditText treeComments;
    private CheckBox check1; 
    private CheckBox check2; 
    private CheckBox check3; 
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.observationformdd);//layout 1.5
	    ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	    ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingsScreenIcon);
	    
	    pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
	    String email = pref.getString("userEmail", null);
	    newObs = Observation.getInstance(email);
	    
	    nut_Tree_Group = (RadioGroup) findViewById(R.id.nut_Tree_Group);
	    seed_Tree_Group = (RadioGroup) findViewById(R.id.seed_Tree_Group);
	    fruit_Tree_Group = (RadioGroup) findViewById(R.id.fruit_Tree_Group);
	    tiny_Tree_Group = (RadioGroup) findViewById(R.id.tiny_Tree_Group);
	    cone_Tree_Group = (RadioGroup) findViewById(R.id.cone_Tree_Group);
	    check1 = (CheckBox) findViewById(R.id.checkbox1);
	    check2 = (CheckBox) findViewById(R.id.checkbox2);
	    check3 = (CheckBox) findViewById(R.id.checkbox3);
	    
	    /* This does not update Observation.java, if you want a default entry you must also enter it in the class manually*/
	    
	    String nut = newObs.getNutTrees();
	    if(nut.equals("YES")) nut_Tree_Group.check(R.id.nut_Tree_Yes);
	    if(nut.equals("NO")) nut_Tree_Group.check(R.id.nut_Tree_No);
	    if(nut.equals("UNSURE")) nut_Tree_Group.check(R.id.nut_Tree_Unsure);
	    
	    String seed = newObs.getSeedTrees();
	    if(seed.equals("YES")) seed_Tree_Group.check(R.id.seed_Tree_Yes);
	    if(seed.equals("NO")) seed_Tree_Group.check(R.id.seed_Tree_No);
	    if(seed.equals("UNSURE")) seed_Tree_Group.check(R.id.seed_Tree_Unsure);
	    
	    String cone = newObs.getConTrees();
	    if(cone.equals("YES")) cone_Tree_Group.check(R.id.coniferous_Yes);
	    if(cone.equals("NO")) cone_Tree_Group.check(R.id.coniferous_No);
	    if(cone.equals("UNSURE")) cone_Tree_Group.check(R.id.coniferous_Unsure);
	    
	    String tiny = newObs.getTinyTrees();
	    if(tiny.equals("YES")) tiny_Tree_Group.check(R.id.tiny_Seeded_Yes);
	    if(tiny.equals("NO")) tiny_Tree_Group.check(R.id.tiny_Seeded_No);
	    if(tiny.equals("UNSURE")) tiny_Tree_Group.check(R.id.tiny_Seeded_Unsure);
	    
	    String fruit = newObs.getFruitTrees();
	    if(fruit.equals("YES")) fruit_Tree_Group.check(R.id.fruit_Bearing_Yes);
	    if(fruit.equals("NO")) fruit_Tree_Group.check(R.id.fruit_Bearing_No);
	    if(fruit.equals("UNSURE")) fruit_Tree_Group.check(R.id.fruit_Bearing_Unsure);

	    //((RadioButton) findViewById(R.id.nut_Tree_No)).performClick();
	    //((RadioButton) findViewById(R.id.seed_Tree_No)).performClick();
	    //((RadioButton) findViewById(R.id.fruit_Bearing_No)).performClick();
	    //((RadioButton) findViewById(R.id.tiny_Seeded_No)).performClick();
	    //((RadioButton) findViewById(R.id.coniferous_No)).performClick();
	    //Like So
	    //newObs.setNutTree("NO");
	    //newObs.setSeedTree("NO");
	    //newObs.setFruitTree("NO");
	   // newObs.setTinyTree("NO");
	   // newObs.setConeTree("NO");
	    
	    
	    nextObsButton5 = (Button) findViewById(R.id.nextObsScreen5);
	    completitionStatus = (TextView) findViewById(R.id.textView2);
	    completitionStatus.setText("66% complete");
	    
	    context = getApplicationContext();
	    
	    treeComments = (EditText) findViewById(R.id.treeComments);
	    treeComments.setText(newObs.getOtherTreeComments());
	    
	    check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.nut_Tree_Yes)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.seed_Tree_Yes)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.fruit_Bearing_Yes)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.tiny_Seeded_Yes)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.coniferous_Yes)).setChecked(true);
		        	check2.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
	        	}
            	else
	        	{
	        		if(nut_Tree_Group.getCheckedRadioButtonId()==R.id.nut_Tree_Yes)
	        		{
	        	    nut_Tree_Group.clearCheck();
	        		}
	        		if(seed_Tree_Group.getCheckedRadioButtonId()==R.id.seed_Tree_Yes)
	        		{
	        	    seed_Tree_Group.clearCheck();
	        		}
	        		if(fruit_Tree_Group.getCheckedRadioButtonId()==R.id.fruit_Bearing_Yes)
	        		{
	        	    fruit_Tree_Group.clearCheck();
	        		}
	        		if(cone_Tree_Group.getCheckedRadioButtonId()==R.id.coniferous_Yes)
	        		{
	        	    cone_Tree_Group.clearCheck();
	        		}
	        		if(tiny_Tree_Group.getCheckedRadioButtonId()==R.id.tiny_Seeded_Yes)
	        		{
	        	    tiny_Tree_Group.clearCheck();
	        		}
		    	}
	        }
	    });
	  	    
	    check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.nut_Tree_No)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.seed_Tree_No)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.fruit_Bearing_No)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.tiny_Seeded_No)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.coniferous_No)).setChecked(true);
		        	check1.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	        		if(nut_Tree_Group.getCheckedRadioButtonId()==R.id.nut_Tree_No)
	        		{
	        	    nut_Tree_Group.clearCheck();
	        		}
	        		if(seed_Tree_Group.getCheckedRadioButtonId()==R.id.seed_Tree_No)
	        		{
	        	    seed_Tree_Group.clearCheck();
	        		}
	        		if(fruit_Tree_Group.getCheckedRadioButtonId()==R.id.fruit_Bearing_No)
	        		{
	        	    fruit_Tree_Group.clearCheck();
	        		}
	        		if(cone_Tree_Group.getCheckedRadioButtonId()==R.id.coniferous_No)
	        		{
	        	    cone_Tree_Group.clearCheck();
	        		}
	        		if(tiny_Tree_Group.getCheckedRadioButtonId()==R.id.tiny_Seeded_No)
	        		{
	        	    tiny_Tree_Group.clearCheck();
	        		}

	        	} 
	        }
	    });

	    check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.nut_Tree_Unsure)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.seed_Tree_Unsure)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.fruit_Bearing_Unsure)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.tiny_Seeded_Unsure)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.coniferous_Unsure)).setChecked(true);
		        	check2.setChecked(false);	    	        		
		        	check1.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	        		if(nut_Tree_Group.getCheckedRadioButtonId()==R.id.nut_Tree_Unsure)
	        		{
	        	    nut_Tree_Group.clearCheck();
	        		}
	        		if(seed_Tree_Group.getCheckedRadioButtonId()==R.id.seed_Tree_Unsure)
	        		{
	        	    seed_Tree_Group.clearCheck();
	        		}
	        		if(fruit_Tree_Group.getCheckedRadioButtonId()==R.id.fruit_Bearing_Unsure)
	        		{
	        	    fruit_Tree_Group.clearCheck();
	        		}
	        		if(cone_Tree_Group.getCheckedRadioButtonId()==R.id.coniferous_Unsure)
	        		{
	        	    cone_Tree_Group.clearCheck();
	        		}
	        		if(tiny_Tree_Group.getCheckedRadioButtonId()==R.id.tiny_Seeded_Unsure)
	        		{
	        	    tiny_Tree_Group.clearCheck();
	        		}
		    
	        	} 
	        }
	    });
	    
	    //setup the gps manager
	    myManager = (LocationManager) getSystemService(LOCATION_SERVICE);	
	    
	    
	    // Create radio group event listeners
	    nut_Tree_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.nut_Tree_Yes:
	        		newObs.setNutTree("YES");
	        		break;
	        	case R.id.nut_Tree_No:
	        		newObs.setNutTree("NO");
	        		break;
	        	case R.id.nut_Tree_Unsure:
	        		newObs.setNutTree("UNSURE");
	        		break;
	        	default: break;
	        	}     	
	        }
	    });
	    
	    seed_Tree_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.seed_Tree_Yes:
	        		newObs.setSeedTree("YES");
	        		break;
	        	case R.id.seed_Tree_No:
	        		newObs.setSeedTree("NO");
	        		break;
	        	case R.id.seed_Tree_Unsure:
	        		newObs.setSeedTree("UNSURE");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    
	    fruit_Tree_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.fruit_Bearing_Yes:
	        		newObs.setFruitTree("YES");
	        		break;
	        	case R.id.fruit_Bearing_No:
	        		newObs.setFruitTree("NO");
	        		break;
	        	case R.id.fruit_Bearing_Unsure:
	        		newObs.setFruitTree("UNSURE");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    
	    tiny_Tree_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.tiny_Seeded_Yes:
	        		newObs.setTinyTree("YES");
	        		break;
	        	case R.id.tiny_Seeded_No:
	        		newObs.setTinyTree("NO");
	        		break;
	        	case R.id.tiny_Seeded_Unsure:
	        		newObs.setTinyTree("UNSURE");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    
	    cone_Tree_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.coniferous_Yes:
	        		newObs.setConeTree("YES");
	        		break;
	        	case R.id.coniferous_No:
	        		newObs.setConeTree("NO");
	        		break;
	        	case R.id.coniferous_Unsure:
	        		newObs.setConeTree("UNSURE");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    
	
		
		


		
		
	    
	    
	    infoIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation5.this, infoMenu.class);
		        startActivity(i);
			}	    	
	    });
	    
	    settingsIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation5.this, settingsMenu.class);
		        startActivity(i);
			}	    	

	    });

	    
	  
	    nextObsButton5.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				newObs.setTreeComments(treeComments.getText().toString());
				Intent i = new Intent(NewObservation5.this, NewObservation6.class);
		        startActivity(i);
				//finish();				
			}
			
	    });

	 

	
     }
     	@Override
	protected void onStart(){
     	// for the dialog indicating the gps is disabled
		super.onStart();
	}
	@Override
	protected void onDestroy() {
		//stopListening();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		//stopListening();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		//startListening();
		super.onResume();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent i;
		switch (item.getItemId()) {
		case PAGE_2:
			i = new Intent(NewObservation5.this, NewObservation2.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_3:
			i = new Intent(NewObservation5.this, NewObservation3.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_4:
			return true;
		case PAGE_5:
			i = new Intent(NewObservation5.this, NewObservation5.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_6:
			i = new Intent(NewObservation5.this, NewObservation6.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_7:
			i = new Intent(NewObservation5.this, NewObservation7.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	super.onCreateOptionsMenu(menu);

	menu.add(PREFERENCES_GROUP_ID, PAGE_2, 0, "Date/Time")
	.setIcon(R.drawable.menuicondatetime);
	menu.add(PREFERENCES_GROUP_ID, PAGE_3, 0, "General")
	.setIcon(R.drawable.menuicongencom);
	menu.add(PREFERENCES_GROUP_ID, PAGE_4, 0, "Setting")
	.setIcon(R.drawable.menuiconsetting);
	menu.add(PREFERENCES_GROUP_ID, PAGE_5, 0, "Trees")
	.setIcon(R.drawable.menuicontree);
	menu.add(PREFERENCES_GROUP_ID, PAGE_6, 0, "Food")
	.setIcon(R.drawable.menuiconfood);
	menu.add(PREFERENCES_GROUP_ID, PAGE_7, 0, "Predators")
	.setIcon(R.drawable.menuiconpred);

	return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK) ) {
	        
	    }
	    return super.onKeyDown(keyCode, event);
	}
	    
}

